// packageC/pages/package/gonzhuangronqiguanli/package_improve/add_improve/add_improve.js
import WxValidate from "../../../../../Wx_Validate/WxValidate"
const mybehavior = require("../../../../../behaviors/requests")
var location_data = {}
var responsibility_data = {}
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/scene_api/fiveS_manage",
    fs_upload_url: "https://logisticsengineering.gree.com/scene_api/photo_manage",
    problem_source: "5S管理",
    problem_source1: "监督检查",
    location: "",
    res_accurate: "",
    problem_category: "",
    problem_description: "",
    res_manager: "",
    res_manager_str: "",
    img_required: false,
    options1: [],
    choose_img_btn_disabled: false,
    files: [],
    res_boj: "",
    company: "",
    manager: "",
    date_show: false,
    submit_btn: false
  },
  // 获取单位负责人
  getDepartmentUserData() {
    let url = this.data.baseUrl + "/get_5s_manager"
    let data = {
      company: this.data.company,
      manager: this.data.res_accurate,
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options1: res.data
        })
      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  setPickerYearValue(e) {
    const now_day = this.gainDate()
    console.log(now_day);
    console.log(e.detail);
    let show_key = now_day.slice(5, 10)
    let show_key1 = e.detail.slice(5, 10)
    if (e.detail < now_day) {
      wx.showToast({
        title: '整改节点需大于当前日期！',
        icon: 'none'
      })
      this.setData({
        rect_deadline: now_day,
        rect_deadline1: show_key,
      })
    } else {
      this.setData({
        rect_deadline: e.detail,
        rect_deadline1: show_key1,
      })
    }

  },

  // 地点
  show_location() {
    this.setData({
      showPicker2: true
    })
  },
  cancel_location() {
    this.setData({
      showPicker2: false
    })
  },
  onConfirm2(e) {
    console.log(e.detail.value);
    this.setData({
      location: e.detail.value[0] + e.detail.value[1] + e.detail.value[2],
      showPicker2: false
    });
  },
  onPicker2Change(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(value, index);

    const c2 = Object.keys(location_data[value[0]]);
    picker.setColumnValues(1, c2);
    if (index == 0) {
      picker.setColumnValues(2, location_data[value[0]][c2[0]]);
    } else {
      picker.setColumnValues(2, location_data[value[0]][value[1]]);
    }

  },
  // 问题类别
  show_problem_category() {
    this.setData({
      showPicker3: true
    })
  },
  cancel_problem_category() {
    this.setData({
      showPicker3: false
    })
  },
  onConfirm3(e) {
    console.log(e.detail);
    let key = e.detail.value
    let arr2 = this.data.problem_type_list[key]
    this.setData({
      problem_category: e.detail.value,
      columns4: arr2,
      problem_description: "",
      showPicker3: false
    });
  },

  // 问题描述
  show_problem_description() {
    this.setData({
      showPicker4: true
    })
  },
  cancel_problem_description() {
    this.setData({
      showPicker4: false
    })
  },
  onConfirm4(e) {
    this.setData({
      problem_description: e.detail.value,
      showPicker4: false
    });
  },
  onPicker5Change(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(index)
    picker.setColumnValues(1, responsibility_data[value[0]]);
  },
  // 下拉选择
  getPickerVlaue(e) {
    console.log(e);
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
    if (key == "res_accurate") {
      this.getDepartmentUserData()
    }
  },
  // 下拉选择
  getPickerVlaue1(e) {
    this.setData({
      res_manager: e.detail.key,
      res_manager_str: e.detail.name
    })
  },
  // 上传照片
  getUploaderVlaue(e) {
    console.log(e);
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
  },
  // 提交
  submit() {
    this.setData({
      submit_btn: true,
    })
    let url = this.data.baseUrl + "/submit_5s_problem"
    let img_arr = []
    for (let i = 0; i < this.data.files.length; i++) {
      img_arr.push(this.data.files[i].file_id);
    }
    let data = {
      problem_source: this.data.problem_source,
      location: this.data.location,
      responsibility: this.data.company + '-' + this.data.res_accurate,
      res_category: this.data.company,
      res_accurate: this.data.res_accurate,
      // 车间负责人
      res_manager: this.data.res_manager,

      problem_category: this.data.problem_category,
      problem_type: this.data.problem_type,
      // 问题描述
      problem_description: this.data.problem_description,
      // 整改要求
      rect_ask: this.data.rect_ask,
      // 整改节点
      rect_deadline: this.data.rect_deadline,
      files: img_arr,
    }
    console.log(data);
    if (!this.WxValidate.checkForm(data)) {
      const error = this.WxValidate.errorList[0]
      this.setData({
        submit_btn: false,
      })
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
    } else {
      this.post(url, data).then(res => {

        if (res.code == 20000) {
          this.setData({
            submit_btn: false,
          })
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          this.reset()
        } else {
          this.setData({
            submit_btn: false,
          })
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        this.setData({
          submit_btn: false,
        })
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 重置表单
  reset() {
    this.setData({
      location: [],
      responsibility: [],
      res_category: [],
      res_accurate: [],
      res_manager: "",
      problem_category: [],
      problem_type: [],
      problem_description: [],
      rect_ask: [],
      rect_deadline1: "",
      rect_deadline: "",
      files: [],
      res_manager_str: "",
    })
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      location: {
        required: true,
      },
      res_accurate: {
        required: true,
      },
      // 车间负责人
      res_manager: {
        required: true,
      },
      problem_category: {
        required: true,
      },
      problem_type: {
        required: true,
      },
      // 问题描述
      // problem_description: {
      //   required: true,
      // },
      // 整改要求
      rect_ask: {
        required: true,
      },
      // 整改节点
      rect_deadline: {
        required: true,
      },
      // 问题照片
      files: {
        required: true,
      },





    }

    let message = {
      location: {
        required: "地点-不能为空",
      },
      res_accurate: {
        required: "责任单位-不能为空",
      },
      res_manager: {
        required: "车间负责人-不能为空",
      },
      problem_category: {
        required: "问题类别-不能为空",
      },

      problem_type: {
        required: "问题类型-不能为空",
      },
      // problem_description: {
      //   required: "问题描述-不能为空",
      // },
      rect_ask: {
        required: "整改要求-不能为空",
      },
      rect_deadline: {
        required: "整改节点-不能为空",
      },
      files: {
        required: "问题照片-不能为空",
      },


    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 获取配置数据
  getConfigDatas() {
    let url = this.data.baseUrl + "/get_5s_problem_config"
    let data = {}
    this.post(url, data).then(res => {
      console.log("配置数据", res.data)

      if (res.code == 20000) {
        location_data = res.data.地点
        let arr = [
          {
            values: Object.keys(location_data),
            className: 'column1',
          },
          {
            values: Object.keys(location_data[Object.keys(location_data)[0]]),
            className: 'column2',
          },
          {
            values: location_data[Object.keys(location_data)[0]][Object.keys(location_data[Object.keys(location_data)[0]])[0]],
            className: 'column3',
            defaultIndex: 2,
          }
        ]
        let arr2 = Object.keys(res.data.责任单位)
        let arr3 = Object.keys(res.data.问题类别)

        this.setData({
          columns3: arr3,
          problem_type_list: res.data.问题类别,
          columns2: arr,
          res_boj: res.data.责任单位.内部单位

        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      company: wx.getStorageSync('company'),
      manager: wx.getStorageSync('department'),
    })

    this.getConfigDatas()
    this.initValidate()
    let childObj = this.selectComponent('.my-calendar')
    childObj.gainDate()
    childObj.dateInit()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})